<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>循环函数中的匿名函数和闭包</title>
</head>
<body>
<header>
    <h1>循环函数中的匿名函数和闭包</h1>
</header>
<main>
    <section>
        <blockquote>每天解决一个bug，进步一点点。。。</blockquote>
        <pre>
            //-------------START------------------
            function fn(){
                var arr = [];
                for(var i = 0; i < 5;i++){
                    arr[i] = "第"+i+1+"个元素";
                }
                return arr;
            }
            fn();
            //----------END-----------------------
            function fn2(){
                var arr = [];
                for(var i=0;i<5;i++){
                    arr[i] = function(){
                        return "第"+i+1+"个元素";
                    }
                }
            }
            fn2();
            fn2()[0];fn2()[1];fn2()[2];
            fn2()[0]();fn2()[1]();fn2()[2]();
            var localfn2 = fn2();
            localfn2[0](); localfn2[1](); localfn2[2]();
            //----------------END------------------
            function fn3(){
                var arr = [];
                for(var i=0;i<5;i++){
                    arr[i] = (function(){
                        return "第"+i+1+"个元素";
                    })()
                }
            }
            fn3();
            fn3()[0];fn3()[1];fn3()[2];
            fn3()[0]();fn3()[1]();fn3()[2]();
            var localfn3 = fn3();
            localfn3[0](); localfn3[1](); localfn3[2]();
            //---------------END--------------------
        </pre>
    </section>
</main>
<footer>
    <a href="javascript:void(0)" onclick="window.close();">关闭</a>
    <abbr>powered by zhangxingong!!!</abbr>
</footer>
</body>
<script type="text/javascript">
    //-------------START------------------
    function fn(){
        var arr = [];
        for(var i = 0; i < 5;i++){
            arr[i] = "第"+i+1+"个元素";
        }
        return arr;
    }
    fn();
    //----------END-----------------------
    function fn2(){
        var arr = [];
        for(var i=0;i<5;i++){
            arr[i] = function(){
                return "第"+i+1+"个元素";
            }
        }
    }
    fn2();
    fn2()[0];fn2()[1];fn2()[2];
    fn2()[0]();fn2()[1]();fn2()[2]();
    var localfn2 = fn2();
    localfn2[0](); localfn2[1](); localfn2[2]();
    //----------------END------------------
    function fn3(){
        var arr = [];
        for(var i=0;i<5;i++){
            arr[i] = (function(){
                return "第"+i+1+"个元素";
            })()
        }
    }
    fn3();
    fn3()[0];fn3()[1];fn3()[2];
    fn3()[0]();fn3()[1]();fn3()[2]();
    var localfn3 = fn3();
    localfn3[0](); localfn3[1](); localfn3[2]();
    //---------------END--------------------
</script>
</html>